/*********************************************************
          File Name:test_unique_ptr.cpp
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Wed 16 Dec 2015 04:29:20 PM CST
**********************************************************/

#include <iostream>
#include "unique_ptr.h"

struct D
{
        template<typename T>
        void operator()(T *d)
        {
                delete d;
                d = nullptr;
        }
};

struct Bar
{
        int x, y;
};

int main(void)
{
        nm::unique_ptr<int> uq(new int(1));

        {
                auto &&x = uq;
                std::cout << *x << "\t" << *uq << std::endl;
                nm::unique_ptr<Bar> y(nullptr);
        }

        nm::unique_ptr<Bar, D> x(new Bar{1, 2}, D());
        nm::unique_ptr<Bar, D> y(std::move(x));
        // crash (use after free)
        std::cout << y->x << "\t" << x->x << std::endl;
        y = nullptr;

        return 0;
}
